package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.User;
import com.graduation.smartparkingplatform.Pojo.UserInfo;
import com.graduation.smartparkingplatform.Pojo.UserNA;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.UserService;
import com.graduation.smartparkingplatform.Utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired UserService userService;
  // 获取用户列表
  @GetMapping("/userList")
  public Response getUserList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<UserNA> userNAList = userService.getUserList(page, count);
      return ResponseUtils.buildSuccess(userNAList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取用户信息
  @GetMapping("/userMess/{userId}")
  public Response getUserMessage(@PathVariable("userId") Integer userId) {
    try {
      UserNA userMess = userService.getUserMessage(userId);
      return ResponseUtils.buildSuccess(userMess);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }
  // 模糊搜索
  @GetMapping("/searchUser")
  public Response searchUser(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "userName") String userName) {
    try {
      PageInfo<UserNA> searchList = userService.searchUser(page, count, userName);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }
  // 增加用户
  @PostMapping("/addUser")
  public Response addUser(@RequestBody User user, HttpServletRequest request) {
    String token = request.getHeader("Authorization");
    Boolean admin = JwtUtils.adminVerifyToken(token);
    if (!admin) {
      return ResponseUtils.buildLOGIN_ERROR("无权限操作");
    } else {
      Boolean addUser = userService.addUser(user);
      if (addUser) {
        return ResponseUtils.buildSuccess("增加用户成功");
      }
      return ResponseUtils.buildError("增加用户失败");
    }
  }

  // 修改用户信息
  @PostMapping("/changeInfo")
  public Response changeInfo(@RequestBody UserInfo userInfo, HttpServletRequest request) {
    String token = request.getHeader("Authorization");
    Boolean admin = JwtUtils.adminVerifyToken(token);
    if (!admin) {
      return ResponseUtils.buildLOGIN_ERROR("无权限操作");
    } else {
      Boolean changeInfo = userService.changeInfo(userInfo);
      if (changeInfo) {
        return ResponseUtils.buildSuccess("修改用户信息成功");
      }
      return ResponseUtils.buildError("修改用户信息失败");
    }
  }

  // 修改用户状态
  @PostMapping("/changeStatus")
  public Response changeUserStatus(
      @RequestParam("userId") Integer userId,
      @RequestParam("status") Boolean status,
      HttpServletRequest request) {
    String token = request.getHeader("Authorization");
    Boolean admin = JwtUtils.adminVerifyToken(token);
    if (!admin) {
      return ResponseUtils.buildLOGIN_ERROR("无权限操作");
    } else {
      Boolean changeStatus = userService.changeUserStatus(userId, status);
      if (changeStatus) {
        return ResponseUtils.buildSuccess("修改用户状态成功");
      }
      return ResponseUtils.buildError("修改用户状态失败");
    }
  }

  // 修改用户管理员权限
  @PostMapping("/changeAdminStatus")
  public Response changeAdminStatus(
      @RequestParam("userId") Integer userId,
      @RequestParam("admin") Boolean adminStatuts,
      HttpServletRequest request) {
    String token = request.getHeader("Authorization");
    Boolean admin = JwtUtils.adminVerifyToken(token);
    if (!admin) {
      return ResponseUtils.buildLOGIN_ERROR("无权限操作");
    } else {
      Boolean changeStatus = userService.changeUserAdmin(userId, adminStatuts);
      if (changeStatus) {
        return ResponseUtils.buildSuccess("修改管理员状态成功");
      }
      return ResponseUtils.buildError("修改管理员状态失败");
    }
  }

  // 删除用户
  @PostMapping("/deleteUser")
  public Response deleteUser(@RequestParam("userId") Integer userId, HttpServletRequest request) {
    String token = request.getHeader("Authorization");
    Boolean admin = JwtUtils.adminVerifyToken(token);
    if (!admin) {
      return ResponseUtils.buildLOGIN_ERROR("无权限操作");
    } else {
      Boolean delete = userService.deleteUser(userId);
      if (delete) {
        return ResponseUtils.buildSuccess("删除用户成功");
      }
      return ResponseUtils.buildError("删除用户失败");
    }
  }
}
